package com.thoughtworks.answer;

import java.util.LinkedList;
import java.util.List;

import com.thoughtworks.exception.NoPathException;
import com.thoughtworks.model.Edge;
import com.thoughtworks.model.Question;
import com.thoughtworks.util.FindTripUtil;

public class Answer extends AbstractAnswer {
	
	
	@Override
	protected void distance_of_fix_trip(Question question) {
		int distance = 0;
		try {
			List<Edge> fix_edges = FindTripUtil.getFixTrip(allEdges, question.getRoute().getRouteString());
			for(Edge edge : fix_edges) {
				distance += edge.getWeight();
			}
			System.out.println(distance);
		} catch (NoPathException e) {
			System.out.println("NO SUCH ROUTE");
		}
		
	}

	@Override
	protected void shortest_distance_of_trip(Question question) {
		try {
			List<List<Edge>> trips = FindTripUtil.getNonredundantTrips(
					allEdges, question.getRoute().getStartNode(), question.getRoute().getEndNode());
			int min_distance = Integer.MAX_VALUE;
			for(List<Edge> trip : trips) {
				int distance = FindTripUtil.calcDistanceOfTrip(trip);
				if(distance < min_distance) {
					min_distance = distance;
				}
			}
			System.out.println(min_distance);
		} catch (NoPathException e) {
			System.out.println("NO SUCH ROUTE");
		}
		
	}

	@Override
	protected void shortest_stop_count_of_trip(Question question) {
		try {
			List<List<Edge>> trips = FindTripUtil.getNonredundantTrips(
					allEdges, question.getRoute().getStartNode(), question.getRoute().getEndNode());
			int min_stops = Integer.MAX_VALUE;
			for(List<Edge> trip : trips) {
				if(trip.size() < min_stops) {
					min_stops = trip.size();
				}
			}
			System.out.println(min_stops);
		} catch (NoPathException e) {
			System.out.println("NO SUCH ROUTE");
		}
		
	}

	@Override
	protected void count_of_trip_as_stop_count_condition(Question question) {
		int stop_count = question.getCompareNum();
		List<List<Edge>> trips = new LinkedList<List<Edge>>();
		try {
			switch(question.getCompareType()) {
			case LESS_THAN:
				stop_count--;         //no break, not bug
			case LESS_THAN_OR_EQUAL:
					trips = FindTripUtil.getTripsWithMaxStopCount(
							allEdges, stop_count, question.getRoute().getStartNode(), question.getRoute().getEndNode());		
				break;
			case EQUAL_TO:
					trips = FindTripUtil.getTripsWithStopCount(
							allEdges, stop_count, question.getRoute().getStartNode(), question.getRoute().getEndNode());
				break;
			default:
				break;
			}
			System.out.println(trips.size());
		} catch(NoPathException e) {
			System.out.println("NO SUCH ROUTE");
		}
	}

	@Override
	protected void count_of_trip_as_distance_condition(Question question) {
		int distance = question.getCompareNum();
		List<List<Edge>> trips = new LinkedList<List<Edge>>();
		try {
			switch (question.getCompareType()) {
			case LESS_THAN:
				distance--;   //no break, not bug
			case LESS_THAN_OR_EQUAL:
					trips = FindTripUtil.getTripsWithMaxDistance(
							allEdges, distance, question.getRoute().getStartNode(), question.getRoute().getEndNode());
				break;
			case EQUAL_TO:
					trips = FindTripUtil.getTripsWithDistance(
							allEdges, distance, question.getRoute().getStartNode(), question.getRoute().getEndNode());
				break;
			default:
				break;
			}
			System.out.println(trips.size());
		} catch(NoPathException e) {
			System.out.println("NO SUCH ROUTE");
		}
			
	}
	
}
